//
//  LoginViewController.swift
//  ddx0513
//
//  Created by ejian  on 15/5/29.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController {

    @IBOutlet weak var phoneTextField: UITextField!
    @IBOutlet weak var phoneShowLabel: UILabel!
    @IBOutlet weak var nextButton: UIButton!
    @IBOutlet weak var phoneShowImg: UIImageView!
    
    var hud: MBProgressHUD?
    var sourceNavi = true
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.phoneShowImg.hidden = true
        self.phoneShowLabel.text = ""
        self.nextButton.enabled = false
        self.phoneTextField.addTarget(self, action: Selector("textFiledDidChange:"), forControlEvents: UIControlEvents.EditingChanged)
//        self.nextButton.setBackgroundImage(UIImage(named: "确定按钮-未激活.png"), forState: UIControlState.Normal)
//        self.nextButton.setBackgroundImage(UIImage(named: "确定按钮-激活.png"), forState: UIControlState.Highlighted)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func nextButtonPressed(sender: AnyObject) {
        self.phoneTextField.resignFirstResponder()
        var str = self.phoneTextField.text
        
        self.startIndicator()
        
        NetUtils.netRequest(Method.GET, URLString: NetUtils.getURLMobileExist(), parameters: ["mobile": str], view: self.view, responseHandler: stopIndicator, successHandler: login)
    }
    
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        if (!self.phoneTextField.exclusiveTouch) {
            self.phoneTextField.resignFirstResponder()
        }
    }
    
    func textFiledDidChange(sender: UITextField) -> Void {
        if (sender == self.phoneTextField) {
            var str = self.phoneTextField.text
            
            if (count(str) > 11) {
                let range = Range(start: str.startIndex, end: advance(str.startIndex, 11))
                self.phoneTextField.text = sender.text.substringWithRange(range)
            }
            
            str = self.phoneTextField.text
            var length = count(str)
            
            switch (length) {
            case 0:
                self.nextButton.enabled = false
                self.phoneShowImg.hidden = true
                self.phoneShowLabel.text = ""
            case 1...3:
                self.nextButton.enabled = false
                self.phoneShowImg.hidden = false
                self.phoneShowLabel.text = str
            case 4...7:
                self.nextButton.enabled = false
                self.phoneShowImg.hidden = false
                str = StringUtils.insertAtIndex(" ", toString: str, atIndex: 3)
                self.phoneShowLabel.text = str
            case 8...10:
                self.nextButton.enabled = false
                self.phoneShowImg.hidden = false
                str = StringUtils.insertAtIndex(" ", toString: str, atIndex: 3)
                str = StringUtils.insertAtIndex(" ", toString: str, atIndex: 8)
                self.phoneShowLabel.text = str
            case 11:
                self.nextButton.enabled = true
                self.phoneShowImg.hidden = false
                str = StringUtils.insertAtIndex(" ", toString: str, atIndex: 3)
                str = StringUtils.insertAtIndex(" ", toString: str, atIndex: 8)
                self.phoneShowLabel.text = str
                
                self.phoneTextField.resignFirstResponder()
            default:
                return
            }
        }
    }
    
    @IBAction func backButtonPressed(sender: AnyObject) {
        self.navigationController?.popViewControllerAnimated(true)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    //MARK: - 网络回调函数
    
    func startIndicator() {
        hud = MBProgressHUD(view: self.view)
        self.view.addSubview(hud!)
        hud?.labelText = "登录中.."
        hud?.show(true)
    }
    
    func stopIndicator() {
        hud?.hide(false)
    }
    
    func login (json: JSON) {
        var isExist = json["isexist"]
        if (isExist) { //号码存在
            var storyBoard = UIStoryboard(name: "Login", bundle: nil)
            var viewController = storyBoard.instantiateViewControllerWithIdentifier("Login2") as! Login2ViewController
            viewController.cellphone = self.phoneTextField.text
            viewController.sourceNavi = self.sourceNavi
            rootController.navigationController?.pushViewController(viewController, animated: true)
        } else { //号码不存在
            var storyBoard = UIStoryboard(name: "Login", bundle: nil)
            var viewController = storyBoard.instantiateViewControllerWithIdentifier("Register") as! RegisterViewController
            viewController.cellphone = self.phoneTextField.text
            viewController.sourceNavi = self.sourceNavi
            rootController.navigationController?.pushViewController(viewController, animated: true)
        }
    }
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if (segue.identifier == "login") {
            var viewController = (segue.destinationViewController as! UINavigationController).viewControllers[0] as! Login2ViewController
            viewController.cellphone = self.phoneTextField.text
        } else if (segue.identifier == "register") {
            var viewController = (segue.destinationViewController as! UINavigationController).viewControllers[0] as! RegisterViewController
            viewController.cellphone = self.phoneTextField.text
            
        }
    }
    */

}
